package cz.muni.fi.pb138.bookinfo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import cz.muni.fi.pb138.bookinfo.R.drawable;

public class FileDialog extends ListActivity {
	private List<String> item = null;
	private List<String> path = null;
	private String root = "/";
	private TextView myPath;		
	private File file;

	/** Called when the activity is first created. 
	 * 	Shows the file explorer.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.folderview);

		myPath = (TextView) findViewById(R.id.pathfile);
		getDir(root);
	}

	private void getDir(String dirPath)	{
		myPath.setText("Location: " + dirPath);
		item = new ArrayList<String>();
		path = new ArrayList<String>();
		
		File f = new File(dirPath);
		File[] files = f.listFiles();

		if (!dirPath.equals(root)) {
			item.add(root);
			path.add(root);
			item.add("../");
			path.add(f.getParent());
		}

		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			path.add(file.getPath());

			if (file.isDirectory()) {
				item.add(file.getName() + "/");
			} else {
				item.add(file.getName());
			}
		}

		ArrayAdapter<String> fileList =	new ArrayAdapter<String>(this, R.layout.row, item);
		setListAdapter(fileList);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		file = new File(path.get(position));

		if (file.isDirectory())	{
			if (file.canRead()) {
				getDir(path.get(position));
			} else {
				new AlertDialog.Builder(this)
				.setIcon(drawable.alert64)
				.setTitle("[" + file.getName() + "] folder can't be read!")
				.setPositiveButton("OK", null).show();
			}
		} else {
			if(isCorrectImage(file.getName())) {			
				new AlertDialog.Builder(this)
				.setIcon(drawable.question64)
				.setTitle(R.string.choose)
				.setMessage("Are you sure you want to choose this file: [" + file.getName() + "] ?")
				.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						new Launcher().setPathToImg(file.getAbsolutePath());	//neni idealni reseni!
						FileDialog.this.finish();
					}
				})
				.setNegativeButton("No", null).show();
			} else {
				new AlertDialog.Builder(this)
				.setIcon(drawable.alert64)
				.setTitle(R.string.wrongformat)
				.setMessage(R.string.formatalert)
				.setPositiveButton("OK", null).show();
			}
		}
	}
	
	/**
	 * Determines whether the selected file is in correct format.
	 * Correct formats are JPG, BMP and GIF.
	 * 
	 * @param file selected file
	 * @return true if file has correct format, false otherwise
	 */
	public boolean isCorrectImage(String file) {
		if(file.endsWith(".jpg") || file.endsWith(".bmp") || file.endsWith(".gif")) {
			return true;
		}
		return false;
	}
}


